home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / c / ljl.exe / LJL.C < prev    next >
C/C++ Source or Header  |  1992-09-12  |  11KB  |  355 lines

  1.  
  2. /*************************************************************************
  3.  *   This program is a combination of the Comp-U-Serve files
  4.  *                   SHADE.TXT
  5.  *                   SPU12.ARC
  6.  *
  7.  *   along with modifications for printing C source code on a
  8.  *   LaserJet Series II.
  9.  *
  10.  *   Since the source and suggestions were public domain, this file and
  11.  *   source are released to public domain.
  12.  *
  13.  *   The source code is for Microsoft C 6.0.
  14.  *
  15.  *   As was said in the files used to create this program:   Enjoy !!!
  16.  *
  17.  *------------------------------------------------------------------------
  18.  *
  19.  * Modified: V1.4 12-Sep-92 by Bob Withers - bwit@delphi.com
  20.  *                                           bwithers@kdsi.com
  21.  *
  22.  *      o  Corrected problem that would fail file open if the filename
  23.  *         provided was not in the current directory. (i.e. LJL A:*.C).
  24.  *
  25.  *      o  Removed all DOS specific code and replaced with my portable
  26.  *         directory search routines.  These directory routines are
  27.  *         copyrighted by me, however I grant permission for them to be
  28.  *         copied and used in any way as long as my copyright statement
  29.  *         is not removed.
  30.  *
  31.  *      o  Modified to use only functions defined by ANSI Standard C.
  32.  *
  33.  *      o  Cleaned up the structure of the code (my opinion).
  34.  *
  35.  *      o  This code has been compiled using Microsoft C/C++ V7.0.  It
  36.  *         has been compiled as both C and C++ code without errors or
  37.  *         warnings.
  38.  *
  39.  *************************************************************************/
  40.  
  41. #include <stdio.h>
  42. #include <stdlib.h>
  43. #include <string.h>
  44. #include <time.h>
  45.  
  46. #include "portdir.h"
  47.  
  48. #define PATH_SEP        '\\'
  49. #define DRIVE_SEP       ':'
  50.  
  51. static void             BuildPathName(char *pszFilePath, char *pszPath,
  52.                                       char *pszName);
  53. static void             blurb(void);
  54. static void             detab(char *str);
  55. static void             get_fdate(time_t *pTime, char *dstr, int nLen);
  56. static void             heading(char *fname, int page, char *dstr);
  57. static void             prn_cmd(char *cmd);
  58. static void             usage(void);
  59. static void             shade(void);
  60. static void             check_line_count(char *pszName);
  61.  
  62.  
  63. /*************************************************************************
  64. Commands for:
  65.    ---------------------------------------------------------------
  66.     H P   L A S E R J E T   S E R I E S   I I   L A N D S C A P E
  67.    ---------------------------------------------------------------
  68.  **************************************************************************/
  69.  
  70. #define LPP         57                          /*  Lines Per Page         */
  71. #define HMW         106                         /*  Header Maximum Width   */
  72. #define PNAME       "HPLJ II (landscape)"       /*  printer description    */
  73. #define SEPCHAR     0xB3                        /*  sep line #'s from text */
  74. #define TMW         88                          /*  Text Maximum Width     */
  75.  
  76. char HDRFONT[]  = {0x1B, 0x28, 0x73, 0x33, 0x54,        /* courier font    */
  77.                    0x1B, 0x26, 0x6B, 0x30, 0x53, 0x00}; /* 10 pitch        */
  78.  
  79. char NO_ULINE[] = {0x1B, 0x26, 0x64, 0x40, 0x00};       /* underline off   */
  80.  
  81. char RESET[]    = {0x1B, 0x45, 0x00};                   /* reset printer   */
  82.  
  83. char SETUP[]    = {0x1B, 0x28, 0x31, 0x30, 0x55,        /* IBM PC char set */
  84.                    0x1B, 0x26, 0x6C, 0x31, 0x4F,        /* landscape       */
  85.                    0x1B, 0x26, 0x6C, 0x38, 0x44, 0x00};    /* 8 lines per inch*/
  86.  
  87. char TXTFONT[]  = {0x1B, 0x26, 0x6B, 0x32, 0x53, 0x00};    /* lineprinter font*/
  88.  
  89. char ULINE[]    = {0x1B, 0x26, 0x64, 0x44, 0x00};       /* underline on    */
  90.  
  91. #define MAXCHAR   512        /* Maximum number of chars per record  */
  92. #define OFF       0
  93. #define ON        1
  94. #define TABVAL    3            /* Tab stops every 3 characters  */
  95. #define VERSION   "1.4"
  96.  
  97. static char         inrec[2 * MAXCHAR], date[16];
  98. static int          len, line, linesout, pnum, status;
  99.  
  100.  
  101. /***********************************************************************/
  102. int main(int argc, char **argv)
  103. {
  104.     auto     FILE           *fpin;
  105.     auto     DIR            *pDir;
  106.     auto     struct dirent  *pDirEnt;
  107.     auto     char            szFilePath[FILENAME_MAX + 1];
  108.  
  109.     blurb();
  110.     if (argc<2)
  111.     {
  112.         usage();
  113.         exit(0);
  114.     }
  115.  
  116.     fflush(stdprn);
  117.     prn_cmd(RESET);
  118.     prn_cmd(SETUP);
  119.     while (--argc)
  120.     {
  121.         pDir = opendir(*++argv);
  122.         if (NULL == pDir)
  123.             continue;
  124.  
  125.         while (NULL != (pDirEnt = readdir(pDir)))
  126.         {
  127.             BuildPathName(szFilePath, *argv, pDirEnt->d_name);
  128.             if ((fpin = fopen(szFilePath, "r")) != NULL)
  129.             {
  130.                 fprintf(stdprn, "\033&a+0L");  /* start with zero left margin  */
  131.                 line = linesout = pnum = 0;        /* initialize counters */
  132.                 get_fdate(&pDirEnt->d_datetime, date, sizeof(date));
  133.                 strupr(*argv);
  134.                 while (fgets(inrec, MAXCHAR, fpin) != NULL)
  135.                 {
  136.                     line++;
  137.                     detab(inrec);
  138.                     len = strlen(inrec);
  139.                     if (pnum == 0)
  140.                         check_line_count(pDirEnt->d_name);
  141.  
  142.                     fprintf(stdprn, "%4d%c  %.*s\r", line, SEPCHAR,
  143.                             TMW - 7, inrec);
  144.                     linesout++;
  145.                     check_line_count(pDirEnt->d_name);
  146.                     while (len > (TMW - 7))
  147.                     {
  148.                         memmove(inrec, &inrec[TMW - 7], len - (TMW - 8));
  149.                         len = strlen(inrec);
  150.                         if ((linesout % LPP) !=0)
  151.                             fprintf(stdprn, "\n");
  152.  
  153.                         fprintf(stdprn, "    %c  %.*s\r", SEPCHAR,
  154.                                 TMW - 7, inrec);
  155.                         linesout++;
  156.                         check_line_count(pDirEnt->d_name);
  157.                     }
  158.  
  159.                     printf("%s  line %d...\r", pDirEnt->d_name, line);
  160.                 }
  161.  
  162.                 fprintf(stdprn,"\f");
  163.                 printf("%s done, %d page%s printed.\n", pDirEnt->d_name,
  164.                         pnum, pnum > 1 ? "s" : "");
  165.                 fclose(fpin);
  166.             }
  167.             else
  168.                 printf("%s - unable to open.\n", pDirEnt->d_name);
  169.         }
  170.     }
  171.  
  172.     prn_cmd(RESET);
  173.     return(0);
  174. }
  175.  
  176.  
  177. /***********************************************************************/
  178.  
  179. static void BuildPathName(char *pszFilePath, char *pszPath, char *pszName)
  180. {
  181.     auto     int        nLen;
  182.  
  183.     nLen = strlen(strcpy(pszFilePath, pszPath));
  184.     while (--nLen >= 0)
  185.     {
  186.         if (PATH_SEP == pszFilePath[nLen] || DRIVE_SEP == pszFilePath[nLen])
  187.         {
  188.             strcpy(&pszFilePath[nLen + 1], pszName);
  189.             return;
  190.         }
  191.     }
  192.  
  193.     strcpy(pszFilePath, pszName);
  194.     return;
  195. }
  196.  
  197.  
  198. /***********************************************************************/
  199.  
  200. static void blurb(void)
  201. {
  202.     printf("HP LaserJet II  -  Source Print Utility - version %s (%s)\n",
  203.            VERSION, __DATE__);
  204.     printf("<<< configured for %s >>>\n\n",PNAME);
  205.     return;
  206. }
  207.  
  208.  
  209. /************************************************************************
  210.  *  routine to test the string for tabs and non-printing ASCII chars and
  211.  *  terminate string with a NULL
  212.  ************************************************************************/
  213.  
  214. static void detab(char *str)
  215. {
  216.     auto     int        cntin, cntout;
  217.     auto     char       tmpstr[2 * MAXCHAR];
  218.  
  219.     cntin = cntout = 0;
  220.     while (cntin < (MAXCHAR + 1) && str[cntin] != '\0' && str[cntin] != '\n')
  221.     {
  222.         if (str[cntin] > 0x1F && str[cntin] < 0x7F)  /* printing chars */
  223.             strcpy(&tmpstr[cntout++], &str[cntin++]);
  224.         else
  225.         {
  226.             switch (str[cntin])
  227.             {
  228.                 case '\t':        /*  check for tab character  */
  229.                     tmpstr[cntout++] = ' ';       /* TAB, fill with spaces */
  230.                     while (cntout % TABVAL)
  231.                         tmpstr[cntout++] = ' ';
  232.  
  233.                     cntin++;
  234.                     break;
  235.  
  236.                 default:
  237.                     tmpstr[cntin++] = 0x2;        /*  print smiley face  */
  238.                     cntout++;
  239.                     break;
  240.             }
  241.         }
  242.     }